package com.azure.bigstore.controller;

import com.azure.bigstore.entity.Token;
import com.azure.bigstore.entity.Wishlist;
import com.azure.bigstore.service.TokenService;
import com.azure.bigstore.service.WishListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created by liuhuaibin on 2018/1/22.
 */
@RestController
@RequestMapping("/v1")
public class WishlistController {
    @Autowired
    private WishListService wishListService;
    @Autowired
    private TokenService tokenService;

    @GetMapping("/wishlist")
    public List<Wishlist> getWishlist(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        List<Wishlist> list = null;
        if(t != null && t != ""){
            Token token = tokenService.findByToken(t);
            //未查询到token
            if(token == null){
                response.setStatus(444);
            }else{
                if(token.getValidityTime().after(new Date())){
                    long time = new Date().getTime()+1800000;//延长30分钟
                    System.out.println("new:"+new Date()+"  time"+new Date(time));
                    token.setValidityTime(new Date(time));
                    tokenService.saveToken(token);

                    list = wishListService.getWishlist(token.getUid());
                }else{
                    response.setStatus(444);
                }
            }
        }else{
            response.setStatus(444);
        }
        return list;
    }

    @GetMapping("/wishlist/{id}")
    public Wishlist getWishlistItem(@PathVariable String id){
        return wishListService.getWishlistItem(id);
    }

    @PostMapping("/wishlist")
    public void addWishlist(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        String p = request.getHeader("p");
        List<Wishlist> list = null;
        if(!"".equals(p) && (t != null && t != "")){
            Token token = tokenService.findByToken(t);
            //未查询到token
            if(token == null){
                response.setStatus(444);
            }else{
                if(token.getValidityTime().after(new Date())){
                    long time = new Date().getTime()+1800000;//延长30分钟
                    System.out.println("new:"+new Date()+"  time"+new Date(time));
                    token.setValidityTime(new Date(time));
                    tokenService.saveToken(token);

                    Wishlist wishlist = new Wishlist(Long.valueOf(p),token.getUid());
                    wishListService.saveWishlistItem(wishlist);
                }else{
                    response.setStatus(444);
                }
            }
        }else{
            response.setStatus(444);
        }
    }

    @DeleteMapping("/onewishlist/{id}")
    public void deleteoneWishlist(@PathVariable String id){
          wishListService.deleteWishlistItem(id);
    }

    @DeleteMapping("/wishlist")
    public void deleteWishlist(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        String p = request.getHeader("p");
        String[] pids = p.split(",");
        System.out.println("--> pids:"+pids);
        for(int i = 0; i < pids.length; i ++){
            wishListService.deleteWishlistItem(pids[i]);
        }
    }
}
